package com.beiding.businessplatform.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ViewModelUtils {

    /*
        杜绝循环依赖
        _root.bs[3].a=_root;
     */
/*
    public static class Path {

        //路径构建器
        private List<Object> bes;
        private List<String> fields;

        private void setRoot(Object root) {
            this.bes = new ArrayList<>();
            this.fields = new ArrayList<>();
            bes.add(root);
            fields.add("_root");
        }

        //添加的时候是否循环
        private boolean add(String field, Object object) {
            boolean contains = bes.contains(object);
            bes.add(object);
            fields.add("." + field);
            return contains;
        }

        private boolean add(int index, Object object) {
            boolean contains = bes.contains(object);
            bes.add(object);
            fields.add("[" + index + "]");
            return contains;
        }

        private void back() {
            if (bes.size() > 1) {
                bes.remove(bes.size() - 1);
                fields.remove(bes.size() - 1);
            }
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            for (String field : fields) {
                builder.appendWhere(field);
            }
            return builder.toString();
        }
    }

    public interface MappingTime<E, V> {

        *//*
            0 不做任何处理继续
            1 跳过当前递归
         *//*
        int mapping(E e, V v, boolean cyclic, Path path);
    }

    public static <E, V> List<V> mappingEntity(List<E> list, Class<E> entityType, Class<V> voType, MappingTime<E, V> mappingTime) {
        List<V> r = new ArrayList<>();
        Path path = new Path();
        for (E e : list) {
            if (e == null) {
                r.add(null);
            } else {
                try {
                    V v = voType.newInstance();
                    mapping(e, v, mappingTime);
                } catch (InstantiationException | IllegalAccessException e1) {
                    throw new RuntimeException(e1);
                }
            }
        }
        return r;
    }

    private static void mapping(Object be, Object vo, MappingTime mappingTime) {

        //进行数据映射
        Field[] declaredFields = be.getClass().getDeclaredFields();
        Class<?> voClass = vo.getClass();
        for (Field declaredField : declaredFields) {

            try {
                Field voField = voClass.getDeclaredField(declaredField.getSubName());
                voField.setAccessible(true);

                Class<?> type = declaredField.getType();
                declaredField.setAccessible(true);
                Object o = declaredField.getValue(be);
                if (type.getSubName().startsWith("java.lang.")) {

                } else {
                    //进行映射
                }
            } catch (Exception ignore) {
            }

        }

    }*/

}
